package com.jay.tools.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Map;

public class SharedUtil {

	private static SharedPreferences sp = null;
	private static final String FILE_NAME = "share_data";

	public SharedUtil() {
		throw new UnsupportedOperationException("cannot be instantiated");
	}

	public static void initSharedPre(Context context) {
		if (sp == null) {
			sp = context.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE);
		}
	}

	/************ TODO put **************/
	public static void putString(String key, String value) {
		Editor edit = sp.edit();
		edit.putString(key, value);
		SharedPreferencesCompat.apply(edit);
	}

	public static void putBoolean(String key, boolean value) {
		Editor edit = sp.edit();
		edit.putBoolean(key, value);
		SharedPreferencesCompat.apply(edit);
	}

	public static void putInt(String key, int value) {
		Editor edit = sp.edit();
		edit.putInt(key, value);
		SharedPreferencesCompat.apply(edit);
	}

	public static void putFloat(String key, float value) {
		Editor edit = sp.edit();
		edit.putFloat(key, value);
		SharedPreferencesCompat.apply(edit);
	}

	public static void putLong(String key, long value) {
		Editor edit = sp.edit();
		edit.putLong(key, value);
		SharedPreferencesCompat.apply(edit);
	}

	/************ TODO get **************/
	public static String getString(String key, String defValue) {
		return sp.getString(key, defValue);
	}

	public static boolean getBoolean(String key, boolean defValue) {
		return sp.getBoolean(key, defValue);
	}

	public static int getInt(String key, int defValue) {
		return sp.getInt(key, defValue);
	}

	public static float getFloat(String key, float defValue) {
		return sp.getFloat(key, defValue);
	}

	public static long getLong(String key, long defValue) {
		return sp.getLong(key, defValue);
	}

	/************ TODO 其它操作 **************/
	/**
	 * @des 移除某个key值已经对应的值
	 * @param key
	 */
	public static void remove(String key) {
		Editor editor = sp.edit();
		editor.remove(key);
		SharedPreferencesCompat.apply(editor);
	}

	/**
	 * @des 清除所有数据
	 */
	public static void clear() {
		Editor editor = sp.edit();
		editor.clear();
		SharedPreferencesCompat.apply(editor);
	}

	/**
	 * @des 查询某个key是否已经存在
	 * @param key
	 * @return
	 */
	public static boolean hasKey(final String key) {
		return sp.contains(key);
	}

	/**
	 * @des 返回所有的键值对
	 * @return
	 */
	public static Map<String, ?> getAll() {
		return sp.getAll();
	}

	/**
	 * TODO 解决SharedPreferencesCompat.apply方法的一个兼容内部类
	 */
	private static class SharedPreferencesCompat {

		private static final Method sAPPLYMETHOD = findApplyMethod();

		/**
		 * @des 反射查找apply的方法
		 * @return
		 */
		@SuppressWarnings({ "unchecked", "rawtypes" })
		private static Method findApplyMethod() {
			try {
				Class clz = Editor.class;
				return clz.getMethod("apply");
			} catch (NoSuchMethodException e) {
			}
			return null;
		}

		/**
		 * @des 如果找到则使用apply执行，否则使用commit
		 * @param editor
		 */
		public static void apply(Editor editor) {
			try {
				if (sAPPLYMETHOD != null) {
					sAPPLYMETHOD.invoke(editor);
					return;
				}
			} catch (IllegalArgumentException e) {
			} catch (IllegalAccessException e) {
			} catch (InvocationTargetException e) {
			}
			editor.commit();
		}
	}

}